package myservlet.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mybean.data.login;

public class changeInfo extends HttpServlet{
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		try {//加载驱动
			Class.forName("com.mysql.jdbc.Driver");	
		}
		catch(Exception e) {}
	}
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=UTF-8");
		PrintWriter out = resp.getWriter();
		login loginBean =null; 
		HttpSession session= req.getSession(true);
		//获取用户更改后的参数  
        String phone = req.getParameter("phone");  
        String money = req.getParameter("money");  
        String deposit = req.getParameter("deposit"); 
        System.out.println(money);
        System.out.println(deposit);
        if(phone==""||money==""||deposit=="") {
			out.println("<script language = javascript>alert('请完善信息!');");
			out.println("location.href='homepage.jsp'</script>");
			return;
		}
		try {
			loginBean = (login)session.getAttribute("loginBean");
			boolean b=loginBean.getLogname()==null || loginBean.getLogname().length()==0;
			if(b) {
				out.println("<script language = javascript>alert('请重新登录!');");
				out.println("location.href='index.jsp'</script>");
				return;
			}
			//修改数据库中用户的电话、余额和租金                                                                                                                                  
			String url = "jdbc:mysql://127.0.0.1:3306/bike?&useSSL=false&serverTimezone=GMT%2B8";
			Connection con = null;
			PreparedStatement sql = null;
			try {
				// 1. 建立与数据库的连接 
				con = DriverManager.getConnection(url,"root","744615");
				String condition="update user set Utel=?,Umoney=?,Udeposit=? where Nickname=?"; 
				sql=con.prepareStatement(condition);
				sql.setString(1, phone);  
				sql.setString(2, money);
				sql.setString(3, deposit);
				sql.setString(4, loginBean.getLogname());
				sql.executeUpdate();                  
				sql.close();
				//修改 loginBean中的信息 
				loginBean.setPhone(phone);  
				loginBean.setmoney(money);  
				loginBean.setdeposit(deposit); 
				//保存到request中
				req.setAttribute("loginBean",loginBean);  
				out.println("<script language = javascript>alert('修改成功!');");
				out.println("location.href='homepage.jsp'</script>");
			}
			catch (SQLException exp){
				out.println("<script language = javascript>alert('数据库更新失败，修改失败!');");
				out.println("location.href='homepage.jsp'</script>");
				System.out.println(exp);
			}
	        con.close();   
		}
		catch (Exception exp){
			System.out.println(exp);
			out.println("<script language = javascript>alert('修改失败!');");
			out.println("location.href='homepage.jsp'</script>");
		}	
	}
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
}
